package com.easy.cloud.web.service.upms.biz.controller;

import com.easy.cloud.web.component.core.response.HttpResult;
import com.easy.cloud.web.component.log.annotation.SysLog;
import com.easy.cloud.web.component.log.annotation.SysLog.Action;
import com.easy.cloud.web.service.upms.api.dto.TenantDTO;
import com.easy.cloud.web.service.upms.api.dto.TenantQueryDTO;
import com.easy.cloud.web.service.upms.api.vo.TenantVO;
import com.easy.cloud.web.service.upms.biz.service.ITenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Tenant API
 *
 * @author Fast Java
 * @date 2023-08-03 15:00:02
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "tenant")
@Api(value = "租户管理", tags = "租户管理")
public class TenantController {

    private final ITenantService tenantService;

    /**
     * 新增
     *
     * @param tenantDTO 新增数据
     * @return 新增数据
     */
    @PostMapping(value = "save")
    @PreAuthorize("@pms.hasPermission('system:tenant:add')")
    @SysLog(value = "租户新增", action = Action.ADD)
    @ApiOperation(value = "租户新增")
    public HttpResult<TenantVO> save(@Validated @RequestBody TenantDTO tenantDTO) {
        return HttpResult.ok(tenantService.save(tenantDTO));
    }

    /**
     * 更新
     *
     * @param tenantDTO 新增数据
     * @return 更新数据
     */
    @PostMapping(value = "update")
    @PreAuthorize("@pms.hasPermission('system:tenant:update')")
    @SysLog(value = "租户更新", action = Action.UPDATE)
    @ApiOperation(value = "租户更新")
    public HttpResult<TenantVO> update(@Validated @RequestBody TenantDTO tenantDTO) {
        return HttpResult.ok(tenantService.update(tenantDTO));
    }

    /**
     * 冻结租户
     *
     * @param tenantId 租户ID
     * @return 更新数据
     */
    @PostMapping(value = "freeze/{tenantId}")
    @PreAuthorize("@pms.hasPermission('system:tenant:update')")
    @SysLog(value = "租户冻结", action = Action.UPDATE)
    @ApiOperation(value = "租户冻结")
    public HttpResult<TenantVO> update(@PathVariable String tenantId) {
        return HttpResult.ok(tenantService.freezeTenant(tenantId));
    }

    /**
     * 根据ID移除数据
     *
     * @param tenantId ID
     * @return 是否删除成功
     */
    @GetMapping(value = "remove/{tenantId}")
    @PreAuthorize("@pms.hasPermission('system:tenant:delete')")
    @SysLog(value = "租户删除", action = Action.DELETE)
    @ApiOperation(value = "租户删除")
    public HttpResult<Boolean> removeById(@PathVariable @NotBlank(message = "当前ID不能为空") String tenantId) {
        return HttpResult.ok(tenantService.removeById(tenantId));
    }

    /**
     * 根据ID获取详情
     *
     * @param tenantId ID
     * @return 详情数据
     */
    @GetMapping(value = "detail/{tenantId}")
    @PreAuthorize("@pms.hasPermission('system:tenant:query')")
    @SysLog(value = "租户详情", action = Action.FIND)
    @ApiOperation(value = "租户详情")
    public HttpResult<TenantVO> detailById(@PathVariable @NotBlank(message = "当前ID不能为空") String tenantId) {
        return HttpResult.ok(tenantService.detailById(tenantId));
    }

    /**
     * TODO 所有数据列表，查询参数自定义
     *
     * @return 查询列表
     */
    @GetMapping(value = "list")
    @PreAuthorize("@pms.hasPermission('system:tenant:query')")
    @SysLog(value = "租户列表", action = Action.FIND)
    @ApiOperation(value = "租户列表")
    public HttpResult<List<TenantVO>> list(TenantQueryDTO tenantQueryDTO) {
        return HttpResult.ok(tenantService.list(tenantQueryDTO));
    }

    /**
     * 根据条件查询分页数据，查询参数自定义
     *
     * @param tenantQueryDTO 查询参数
     * @return 查询分页数据
     */
    @GetMapping(value = "page")
    @PreAuthorize("@pms.hasPermission('system:tenant:query')")
    @SysLog(value = "租户分页", action = Action.FIND)
    @ApiOperation(value = "租户分页")
    public HttpResult<Page<TenantVO>> page(TenantQueryDTO tenantQueryDTO) {
        return HttpResult.ok(tenantService.page(tenantQueryDTO));
    }
}